import areaList from '../../utils/areaList';
const app = getApp()

Page({

  data: {
    //消息列表数据
    noticeList: [],
    //页码
    pageNum: 1,
    key_word: "",
    areaList: areaList,
    TabCur: 1,
    scrollLeft: 0,
    area: '地区',
    grade: '等级',
    grade_value: 1,
    goods_list: [],
    sift_obj: {
      free_shipping: false,
      is_hot: 0,
      province: "",
      city: "",
      position: "",
      cat_id: ""
    },
    sift_option_1: [{
        text: '综合',
        value: '1'
      },
      {
        text: '销量',
        value: '2'
      }
    ],
    sift_option_2: [{
        text: '价格升序',
        value: '2'
      },
      {
        text: '价格降序',
        value: '3'
      }
    ],
    category_info: [{
        "id": 8,
        "title": "A+",
        "pid": 0,
        "level": 1,
        "order": 1,
        "add_time": 1606272530,
        "status": 1,
        "path": ",0,",
        "hot": 0
      },
      {
        "id": 7,
        "title": "A",
        "pid": 0,
        "level": 1,
        "order": 2,
        "add_time": 1606272550,
        "status": 1,
        "path": ",0,",
        "hot": 0
      },
      {
        "id": 9,
        "title": "A-",
        "pid": 0,
        "level": 1,
        "order": 3,
        "add_time": 1606272573,
        "status": 1,
        "path": ",0,",
        "hot": 0
      },
      {
        "id": 12,
        "title": "B+",
        "pid": 0,
        "level": 1,
        "order": 4,
        "add_time": 1606272583,
        "status": 1,
        "path": ",0,",
        "hot": 0
      },
      {
        "id": 10,
        "title": "B",
        "pid": 0,
        "level": 1,
        "order": 5,
        "add_time": 1606272583,
        "status": 1,
        "path": ",0,",
        "hot": 0
      },
      {
        "id": 13,
        "title": "B-",
        "pid": 0,
        "level": 1,
        "order": 6,
        "add_time": 1606272583,
        "status": 1,
        "path": ",0,",
        "hot": 0
      },
      {
        "id": 15,
        "title": "C+",
        "pid": 0,
        "level": 1,
        "order": 6,
        "add_time": 1606272583,
        "status": 1,
        "path": ",0,",
        "hot": 0
      },
      {
        "id": 14,
        "title": "C",
        "pid": 0,
        "level": 1,
        "order": 6,
        "add_time": 1606272583,
        "status": 1,
        "path": ",0,",
        "hot": 0
      },
      {
        "id": 16,
        "title": "C-",
        "pid": 0,
        "level": 1,
        "order": 6,
        "add_time": 1606272583,
        "status": 1,
        "path": ",0,",
        "hot": 0
      }
    ]

  },
  //到达底部
  scrollToLower: function (e) {
    if (!this.data.loading && !this.data.noMore){
      this.setData({
        loading: true,
        pageNo: this.data.pageNo + 1
      })
      this.getData(true);
    }
  },





  price_change(e) {
    console.log(e);
  },


  //校验缓存中的token 和 通过openid拿到的token进行比对
  check_token() {
    var that = this
    var user_info_token = wx.getStorageSync('token');
    //获取缓存中的openid或者是全局当中的openid变量 通过指定openid查询后台的token 
    wx.request({
      url: 'https://vip.join.ink/api/get_token',
      data: {
        openid: wx.getStorageSync('openid'), //获取缓存中的openid openid不会变
        token: user_info_token
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        if (res.data.code == 200) {
          //此时重新设置新的token作为缓存对象
          wx.setStorageSync('token', res.data.api_token)
          wx.setStorageSync('token_is_agreement', res.data.data) //一致情况下为1
        } else {
          //缓存中的token和后台当中的token不一致  需要重新授权登录
          wx.removeStorageSync('token')
          if (user_info_token != '') {
            wx.showModal({
              title: '提示',
              content: 'token失效，请重新登录！',
              showCancel: false
            })
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/personal/personal',
              })
            }, 2000);
          }

        }
      }
    })
  },
  free_shipping_change(e) {
    this.setData({
      'sift_obj.free_shipping': e.detail
    });
  },
  go_map() {
    wx.navigateTo({
      url: '/pages/map/map',
    })
  },


  onClose() {
    this.fetch()
    console.log(this.data.sift_obj);
  },

  grade_change(e) {
    console.log(e);
    let grade = this.data.category_info[e.detail].id
    this.setData({
      "sift_obj.cat_id": grade
    })
    console.log(this.data.sift_obj);
    this.fetch()
  },

  area_change(e) {
    let area = e.detail.values[2].name
    let province = e.detail.values[0].name
    let city = e.detail.values[1].name
    let position = e.detail.values[2].name
    console.log(province);
    if (province == "全部") {
      province = ""
      area = "全部"
    }
    this.setData({
      area: area,
      'sift_obj.province': province,
      'sift_obj.city': city,
      'sift_obj.position': position
    })
    this.fetch()
  },

  fetch() {
    this.get_goods_list(this.data.sift_obj)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this
    console.log(res, '88888888888888888888')
    if (res.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(res.target);
    } else {
      var pages = getCurrentPages() //获取加载的页面
      var currentPage = pages[pages.length - 1] //获取当前页面的对象
      var url = currentPage.route //当前页面url
      console.log(url, '888888888888')
      console.log("来自右上角转发菜单")
      return {
        title: '分享至朋友圈',
        path: url,
        success: (res) => {
          console.log("转发成功", res);
        },
        fail: (res) => {
          console.log("转发失败", res);
        }
      }
    }

  },
  get_goods_list(sift) {
    app.request('/online_getsaleList', {
      data: sift
    }).then(res => {
      console.log(res, "商品列表1156516551515");
      if (res.data.code == 200) {
        this.setData({
          goods_list: res.data.sale_info.data
        })
      }
    }).catch(err => {
      console.log(err);
    })
  },

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  tabSelect(e) {
    console.log(e);
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },


  onLoad: function (options) {
    this.fetch()
    wx.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log(res)
      },
      fail(e) {
        console.log(e)
      }
    })
  },

  onShow: function () {
    this.fetch()
    this.check_token()
  }




})